//
//  AppDelegate.m
//  LbAdDemo
//
//  Created by SX on 2021/9/15.
//

#import "AppDelegate.h"
#import "NavigationViewController.h"
#import "ViewController.h"
#import "Config.h"
#import <LbAdSdk/ADv.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/ASIdentifierManager.h>

static NSString *const AppPlacementID = @"a61234e1cb6a76";
static NSString *const AppChannel = @"spx_XH";

@interface AppDelegate () <SplashDelegate>
@property (nonatomic, strong) UIImageView* maskImageView;
@property (nonatomic, strong) SplashAD* splashAD;
@end


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //初始化广告sdk
    [ADv debug:YES];
    [ADv initApp:LB_TEST_APPID channel:LB_TEST_CHANNEL tracking:YES]; //渠道号需要填入，需要联系商务
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.rootViewController = [[NavigationViewController alloc] initWithRootViewController:[ViewController new]];
    [_window makeKeyAndVisible];
    
    //添加遮罩
    _maskImageView = [[UIImageView alloc] initWithFrame:_window.bounds];
    _maskImageView.backgroundColor= [UIColor whiteColor];
    _maskImageView.userInteractionEnabled = YES;
    [_window addSubview:_maskImageView];
    
    [self initAds];
    return YES;
}


#pragma mark - UISceneSession lifecycle


//- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
//    // Called when a new scene session is being created.
//    // Use this method to select a configuration to create the new scene with.
//    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
//}


//- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
//    // Called when the user discards a scene session.
//    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
//    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
//}

// ios 15 申请权限处理
-(void)initAds
{
    UIApplication *applicaiton = [UIApplication sharedApplication];

    if (applicaiton.applicationState == UIApplicationStateActive)
    {
        if (@available(iOS 14, *))
        {
            ATTrackingManagerAuthorizationStatus status = [ATTrackingManager trackingAuthorizationStatus];

            if(status == ATTrackingManagerAuthorizationStatusNotDetermined)
            {
                [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status)
                {
                    // Tracking authorization completed. Start loading ads here.

                    [[NSOperationQueue mainQueue] addOperationWithBlock:^
                    {
                        //显示开屏广告
                        self->_splashAD = [ADv showSp:LB_TEST_SP_ID containerView:self->_window delegate:self];
                    }];
                }];
            }
            else
            {
                _splashAD = [ADv showSp:LB_TEST_SP_ID containerView:_window delegate:self];
            }
        }
        else
        {
            _splashAD = [ADv showSp:LB_TEST_SP_ID containerView:_window delegate:self];
        }
    }
    else
    {
        [self performSelector:@selector(initAds)
                       withObject:nil
                       afterDelay:0.5f];
    }
}

// MARK:- loading

- (void)didFailToLoadADWithPlacementID:(NSString *)placementID error:(NSError *)error {
    [_maskImageView removeFromSuperview];
}

- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
    
}

// MARK:- splash delegate

- (void)splashDidShowForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra {
    [_maskImageView removeFromSuperview];
}

@end
